<?php
/**
 * Created by PhpStorm.
 * User: lzy
 * Date: 2018/11/7
 * Time: 9:51
 */

namespace App\FreeBird\Repositories;

use App\FreeBird\Contracts\InfoClassInterface;
use App\FreeBird\Models\InfoClassModel;
use Illuminate\Support\Facades\Request;

class  DbInfoClassRepositories implements InfoClassInterface
{
    /**
     * @return InfoClassModel
     */
    public function getInfoClassModel()
    {
        return app()->make(InfoClassModel::class);
    }


    /**
     * 获取菜单列表
     * @param $where
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|mixed
     */
    public function getClassMenuList($where)
    {
        $request = Request::instance();
        $limit = empty($request->input("limit")) ? 10 : $request->input("limit");

        return $this->getInfoClassModel()->newQuery()->where($where)->paginate($limit);
    }

    /**
     * 获取信息列表
     * @param $where
     * @return mixed|void
     */
    public function getInfo($where)
    {
        return $this->getInfoClassModel()->newQuery()->where($where)->first();
    }


    /**
     * 删除数据
     * @param $data
     * @return mixed|void
     */
    public function delData($where)
    {
        return $this->getInfoClassModel()->newQuery()->where($where)->delete();
    }

    /**
     * 配置添加与更新
     * @param $data
     * @return mixed|void
     */
    public function updateOrCreateClassClassData($data)
    {
        if (!empty($data["service_method"])) {
            $data["service_method"] = implode(",", $data["service_method"]);
        } else {
            $data["service_method"] = "";
        }

        $data['status'] = empty($data["status"]) ? 0 : 1;
        $data['recommended'] = empty($data["recommended"]) ? 0 : 1;
        $id = $data["id"];
        unset($data["id"]);
        $data = $this->getInfoClassModel()->filter($data);
        return $this->getInfoClassModel()->newQuery()->updateOrCreate(["id" => $id], $data);
    }

    /***
     * 获取分类数据
     * @param $where
     * @return mixed|void
     */
    public function getClassAll($where)
    {
        return $this->getInfoClassModel()->newQuery()->select(["id", "name", "pid", 'logo', 'service_method', 'minimum_bounty', 'recommended'])->where($where)->get();
    }

    /***
     * 获取分类数据
     * @param $where
     * @return mixed|void
     */
    public function getClassSubAll($where)
    {
        return $this->getInfoClassModel()->newQuery()->select(["id", "name", "pid", 'logo', 'service_method', 'minimum_bounty', 'recommended'])->where($where)->get();
    }

    /**
     * 获取字段信息
     * @param $where
     * @param $field
     * @return mixed|void
     */
    public function getInfoFieldValue($where, $field)
    {
        return $this->getInfoClassModel()->newQuery()->where($where)->value($field);
    }

    /**
     * 总菜单分类
     * @param $where
     * @return mixed|void
     */
    public function getInfoClassAllMenu($where){
          $classList=$this->getInfoClassModel()->newQuery()->select(["id", "name", "pid", 'logo', 'service_method', 'minimum_bounty', 'recommended'])->where($where)->get();
          $classArr=list_to_tree($classList->toArray());
          return $classArr;
    }
}